package com.yihaodian.index.solr.ext;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.lucene.index.IndexReader;
import org.apache.solr.core.AbstractSolrEventListener;
import org.apache.solr.core.SolrCore;
import org.apache.solr.search.SolrIndexSearcher;

/**
 * 一个单体类，管理本节点上的所有的DataProvider。
 *
 * @author zhouhang
 *
 */
public class MemDataManager extends AbstractSolrEventListener {

	public MemDataManager(SolrCore core) {
		super(core);
	}

	// index -> field -> MemDataProvider
	private static Map<String, Map<String, MemDataProvider>> providers;	
	
	// datadir -> MemDataProvider
	private static Map<String, List<MemDataProvider>> dataDirMap;
	
	public synchronized static MemDataProvider getDataProvider(String indexName, String fieldName, String dataDir) {
		if(providers == null) {
			providers = new HashMap<String, Map<String, MemDataProvider>>();
		}
		Map<String, MemDataProvider> provMap = providers.get(indexName);
		if(provMap == null) {
			provMap = new HashMap<String, MemDataProvider>();
			providers.put(indexName, provMap);
		}
		MemDataProvider provider = provMap.get(fieldName);
		if(provider == null) {
			provider = new MemDataProvider(indexName, fieldName, dataDir);
			provMap.put(fieldName, provider);
		}
		return provider;
	}
	
	
	public synchronized static List<MemDataProvider> getDataProvider(String dataDir) {
		if(dataDirMap == null) return null;
		
		return dataDirMap.get(dataDir);
	}
	
	public synchronized static void registerDataDir(String dataDir, MemDataProvider provider) {
		if(dataDirMap == null) {
			dataDirMap = new HashMap<String, List<MemDataProvider>>();
		}
		List<MemDataProvider> pl = dataDirMap.get(dataDir);
		if(pl == null) {
			pl = new ArrayList<MemDataProvider>();
			dataDirMap.put(dataDir, pl);
		}
		
		pl.add(provider);
	}
	
	public synchronized static void unRegisterDataDir(String dataDir) {
		if(dataDirMap == null) {
			return;
		}
		List<MemDataProvider> pl = dataDirMap.get(dataDir);
		if(pl != null) {
			for(MemDataProvider p : pl) {
				p.closeSource(dataDir);
			}
			dataDirMap.remove(dataDir);	
		}
		
	}
	
	@Override
	public void newSearcher(SolrIndexSearcher newSearcher,
			SolrIndexSearcher currentSearcher) {

		IndexReader reader = newSearcher.getIndexReader();
		String dataDir = newSearcher.getCore().getDataDir();

		if(providers != null && !providers.isEmpty()) {
			for(Map<String, MemDataProvider> provMap: providers.values()) {
				if(provMap != null && !provMap.isEmpty()) {
					for(MemDataProvider provider : provMap.values()) {
						MemFloatSource mfs = provider.getSource(dataDir); 
						if(mfs != null) {
							mfs.loadValues(reader, true);
						}
					}
				}
				
			}
		}

	}


	public static Collection<String> getDataProviders(String indexName) {
		Collection<String> ret = Collections.EMPTY_SET;
		
		if(providers == null || providers.get(indexName) == null) {
			return ret;
		}
		
		ret = providers.get(indexName).keySet();
		return ret;
	}

}
